package gui;

import core.para.Para;
import gui.processor.Processor;
import gui.utils.ProgBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class North extends JPanel implements ActionListener{
    private JButton flowchart,transcripts;

    private ProgBar progA, progB;
    private VTL1 vtl1;

    public North(VTL1 vtl1){
        super(new BorderLayout());
        this.vtl1 = vtl1;

        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        
        JPanel p = new JPanel();
        p.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        p.setLayout(new GridLayout(1,4));
        p.add(getButton(flowchart = new JButton("FLOWCHART"), 130, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Home"));
        p.add(getButton(transcripts = new JButton("TRANSCRIPTS"), 130, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Table"));
        
        this.add(p, BorderLayout.WEST);

        ButtonGroup group = new ButtonGroup();
        group.add(flowchart);
        group.add(transcripts);
        
        JPanel progressAndMessages = new JPanel();
        progressAndMessages.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        progressAndMessages.setLayout(new BorderLayout());
        progA = new ProgBar(Color.DARK_GRAY, 5, 2);
        progB = new ProgBar(Color.LIGHT_GRAY, 1, 4);
        progressAndMessages.add(progA, BorderLayout.NORTH);
        progressAndMessages.add(progB, BorderLayout.SOUTH);

        this.add(progressAndMessages, BorderLayout.EAST);
    }

    public static JPanel getButton(JButton st, int width, int height, Color fCol, Color bCol, ActionListener al, String tooltip){
        st.setForeground(fCol);
        st.setBackground(bCol);
        st.setPreferredSize(new Dimension(width, height));
        st.addActionListener(al);
        st.setToolTipText(tooltip);
        JPanel p = new JPanel(new FlowLayout());
        p.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        p.setOpaque(false);
        p.add(st);
        return p;
    }
    

    
    //public void resetCenterGUI(){segL1.resetCenterGUI();}
    
    public void setNorthTabActiveState(int which){
        resetButtonColors();
        if(which==VTL1.FLOWCHART){
            vtl1.loadCard(VTL1.FLOWCHART);
            vtl1.renderFrontEnd(0);
            flowchart.setForeground(Color.RED);
            vtl1.tickMSG("Use the Start menu to begin.");
        }
        else if(which==VTL1.TRANSCRIPTS){
            if(Processor.tScripts!=null){
                vtl1.loadCard(VTL1.TRANSCRIPTS);
                transcripts.setForeground(Color.RED);
                vtl1.tickMSG("Saved to: " + Para.PATH_TO_OUTPUT);
            }
            else{
                vtl1.loadCard(VTL1.FLOWCHART);
                flowchart.setForeground(Color.RED);
                vtl1.tickMSG("There are no transcripts to show. Use assembly pipeline to create them.");
            }
        }
    }
    
    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if(source == flowchart){setNorthTabActiveState(VTL1.FLOWCHART);}
        else if(source == transcripts){setNorthTabActiveState(VTL1.TRANSCRIPTS);}
    }

    private void resetButtonColors(){
        flowchart.setForeground(new Color(102, 102, 102));
        transcripts.setForeground(new Color(102, 102, 102));
    }
    
    public void idPrgA(int which){
        progA.idPrg(which);
    }
    public void dPrgA(int which, int val){
        progA.dPrg(which, val);
        if(which!=ProgBar.UPDATE){progB.idPrg(which);}
    }
}